﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Windows.Report
{
    public partial class frmNXHoaDonCT : DevExpress.XtraEditors.XtraForm
    {
        string _type = "";
        long id = -1;

        public long Id
        {
            get { return id; }
            set { id = value; }
        }
        DataSet _ds = null;
        public DataSet DataSource { get { return _ds; } }
        public string Type
        {
            get
            {
                if (radioGroup1.Enabled == true)
                    return radioGroup1.EditValue.ToString();
                return radioGroup2.EditValue.ToString();
            }

        }
        public string KyHieuHD
        {
            get { return txtKyHieuDH.Text; }
            set { txtKyHieuDH.Text = value; }
        }
        public string TuSoHD
        {
            get { return txtTuSo.Text; }
            set { txtTuSo.Text = value; }
        }
        public string DenSoHD
        {
            get { return txtDenSo.Text; }
            set { txtDenSo.Text = value; }
        }
        public frmNXHoaDonCT(bool banra)
        {
            InitializeComponent();
            if (banra)
            {
                radioGroup2.Enabled = false;
            }
            else radioGroup1.Enabled = false;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }
        public DataSet BuildDataSourceNXHD()
        {
            string query = @"SELECT     BAOCAOTHUE.ID,BAOCAOTHUE.NGAYPS, BAOCAOTHUE.KYHIEUHD, BAOCAOTHUE.SOHD, BAOCAOTHUE.TENKH, BAOCAOTHUE.MSTHUE, BAOCAOTHUE.HANGHOA, 
                      SUM(BAOCAOTHUE.DOANHSO) AS DOANHSO, MAX(BAOCAOTHUE.THUESUAT) AS THUESUAT, SUM(BAOCAOTHUE.THUEGTGT) AS THUEGTGT, BAOCAOTHUE.DIACHI, 
                      BAOCAOTHUE.SOBT, BAOCAOTHUE.NGAYGD, BAOCAOTHUE.CUSINFO, MASOTHUE.TEN
FROM         BAOCAOTHUE LEFT OUTER JOIN
                      MASOTHUE ON BAOCAOTHUE.MSTHUE = MASOTHUE.MST
WHERE     (BAOCAOTHUE.ID ='{0}')
GROUP BY BAOCAOTHUE.ID,BAOCAOTHUE.NGAYPS, BAOCAOTHUE.KYHIEUHD, BAOCAOTHUE.SOHD, BAOCAOTHUE.TENKH, BAOCAOTHUE.MSTHUE, BAOCAOTHUE.HANGHOA, 
                      BAOCAOTHUE.DIACHI, BAOCAOTHUE.SOBT, BAOCAOTHUE.NGAYGD, BAOCAOTHUE.CUSINFO, MASOTHUE.TEN

SELECT     BAOCAOTHUE.ID, TONHANG.TENHANG, TONHANG.MAHANG, TONHANG.DVTINH, NXHOADON.SOLUONG, NXHOADON.GIABAN, NXHOADON.SOTIEN
FROM         BAOCAOTHUE LEFT OUTER JOIN
                      NXHOADON ON BAOCAOTHUE.ID = NXHOADON.NO LEFT OUTER JOIN
                      TONHANG ON NXHOADON.MAHANG = TONHANG.MAHANG
WHERE     (BAOCAOTHUE.ID = '{0}')

SELECT     TenDV, DiaChi, DienThoai, Fax, MaSoThue, GiamDoc, KeToanTruong, TaiKhoan, NganHang, TinhTP
FROM         HETHONG";
            query = string.Format(query, id);
            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            return _ds;
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            _ds = BuildDataSourceNXHD();
            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}